#-*- codeing = utf-8 -*-
#@Time : 2021/2/3 14:10
#@Author : 李佳恒
#@File : testUrllib.py.py
#@Software: PyCharm

import urllib.request

# data = bytes(urllib.parse.urlencode({"hello":"world"}),encoding="utf-8")

#超时处理
# try:
#     response =  urllib.request.urlopen("http://httpbin.org/get",timeout=1)
#     print(response.read().decode('utf-8'))
# except urllib.error.URLError as e:
#     print("FUCK OFF!")


url =  "https://www.douban.com"
# url =  "https://httpbin.org/post"
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
}
# data = bytes(urllib.parse.urlencode({'name':'eric'}),encoding='utf-8')
# req = urllib.request.Request(url=url , data=data, headers=headers,method="POST")
# respone = urllib.request.urlopen(req)
#
# print(respone.read().decode('utf-8'))
req = urllib.request.Request(url=url, headers=headers)
respone = urllib.request.urlopen(req) #用封装好的对象方式访问url
print(respone.read().decode('utf-8'))